package com.tm.mygsyplayer.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.graphics.Bitmap;
import android.media.MediaMetadataRetriever;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.provider.MediaStore;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.tm.mygsyplayer.R;
import com.tm.mygsyplayer.utils.ScreenUtils;
import com.tm.mygsyplayer.utils.ThumbUtils;

import java.io.File;
import java.util.HashMap;

/**
 * 获取视频的第一帧图片
 */
public class VideoThumbnailActivity extends AppCompatActivity {

    ImageView ivVideoCover;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_video_thumbnail);

        ivVideoCover = findViewById(R.id.iv_video_cover);

        String url = "http://gslb.miaopai.com/stream/Kkfwus-oq0FvDYFSM2ZdUA7fpvF~Eb7AOgaggw__.mp4?vend=miaopai&ssig=985a58a4a43d9dcb51e498d922f0cb60&time_stamp=1568709306250&mpflag=32&unique_id=1568705667098034";
        String qUrl = "http://img.xmy91.com/%E7%BB%B4%E5%A4%9A%E5%88%A9%E4%BA%9A%E9%93%81%E6%9D%BF%E7%83%A7%20%E9%87%8D%E8%B0%83%E8%89%B2.mp4";
        String title = "测试视频";

        // 将列表中的每个视频设置为默认16:9的比例   宽:高 = 16:9
        ViewGroup.LayoutParams params = ivVideoCover.getLayoutParams();
        params.width = ScreenUtils.getScreenWidth(this); // 宽度为屏幕宽度
        params.height = (int) (params.width * 9f / 16f);    // 高度为宽度的9/16
        ivVideoCover.setLayoutParams(params);

        ivVideoCover.setImageResource(R.mipmap.ic_video_cover);

//        Bitmap bitmap = ThumbUtils.getVideoThumbnail(url, MediaStore.Images.Thumbnails.MINI_KIND);
//        Bitmap bitmap = ThumbUtils.getLocalVideoThumbnail(url);
//        ivVideoCover.setImageBitmap(bitmap);

        //new VideoThumbTask().execute(qUrl);
//        new Thread(new Runnable() {
//            @Override
//            public void run() {
//                Bitmap bi = ThumbUtils.getVideoThumbnail(url, MediaStore.Images.Thumbnails.MINI_KIND);
//
//                Message message = mHanlder.obtainMessage();
//                message.what = MSG_THUMB;
//                message.obj = bi;
//                mHanlder.sendMessage(message);
//            }
//        }).start();
    }

    private static final int MSG_THUMB = 11;

    private Handler mHanlder = new Handler() {

        @Override
        public void handleMessage(Message msg) {
//            super.handleMessage(msg);
            switch (msg.what) {
                case MSG_THUMB:
                    Bitmap bitmap = (Bitmap) msg.obj;
                    ivVideoCover.setImageBitmap(bitmap);
                    break;
            }
        }
    };

    class VideoThumbTask extends AsyncTask<String, Integer, Bitmap> {

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
        }

        @Override
        protected Bitmap doInBackground(String... strings) {
            String path = strings[0];
//            Bitmap bitmap = ThumbUtils.getVideoThumbnail(path, MediaStore.Images.Thumbnails.MINI_KIND);
            Bitmap bitmap = getBitmap(VideoThumbnailActivity.this, path, false);
            return bitmap;
        }

        @Override
        protected void onPostExecute(Bitmap bitmap) {
            super.onPostExecute(bitmap);
            ivVideoCover.setImageBitmap(bitmap);
        }

    }

    public Bitmap getBitmap(Context context, String url, boolean isSD) {
        Bitmap bitmap = null;
        //MediaMetadataRetriever 是android中定义好的一个类，提供了统一
        //的接口，用于从输入的媒体文件中取得帧和元数据；
        MediaMetadataRetriever retriever = new MediaMetadataRetriever();
        try {
            if (isSD) {
                //（）根据文件路径获取缩略图
                retriever.setDataSource(context, Uri.fromFile(new File(url)));
            } else {
                //根据网络路径获取缩略图
                retriever.setDataSource(url, new HashMap<>());
            }
            //获得第一帧图片
            bitmap = retriever.getFrameAtTime();
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } finally {
            retriever.release();
        }
        return bitmap;
    }

}
